package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.pojo.Paper;
import com.hqyj.dao.PaperMapper;
import com.hqyj.pojo.PaperQuestion;
import com.hqyj.pojo.PaperQuestionRel;
import com.hqyj.pojo.ResultVo;
import com.hqyj.service.IPaperQuestionRelService;
import com.hqyj.service.IPaperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-09-15
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements IPaperService {

    @Resource
    PaperMapper paperMapper;

    //注入试卷和试题中间表的 service实现类
    @Autowired
    IPaperQuestionRelService iPaperQuestionRelService;

    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Transactional //添加事务
    @Override
    public ResultVo paperCreate(Paper p) {
        ResultVo rv = new ResultVo();
        //验证试卷名称不能重名
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_name",p.getPaperName());
        Paper pp = paperMapper.selectOne(queryWrapper);
        if(pp!=null){
            rv.setInfo("试卷名称重名");
            return rv;
        }
        //试卷新增
        int num = paperMapper.insert(p);

        System.out.println("p"+p);
        if(num>0){
            //把 quIds取出分割
            String[] quIds = p.getQuIds().split(",");
            //创建一个中间表对象集合
            List<PaperQuestionRel> list = new LinkedList<>();
            for(int i=0;i<quIds.length;i++){
                PaperQuestionRel pq = new PaperQuestionRel();
                pq.setPaperId(p.getPaperId());
                pq.setQuestionId(Integer.parseInt(quIds[i]));
                list.add(pq);
            }
            //批量插入
            boolean is = iPaperQuestionRelService.saveBatch(list);
            if(is){
                rv.setInfo("试卷生成成功");
                rv.setSuccess(true);
            }else{
                rv.setInfo("试卷生成失败");
            }
        }else{
            rv.setInfo("试卷生成失败");
        }
        return rv;
    }

    @Override
    public ResultVo queryByName(String paperName) {
        //定义key
        String key ="exam:"+paperName;
        //判断缓存中是否有数据
        Object obj = redisTemplate.opsForValue().get(key);
        Paper p = null;
        if(obj==null){
             p = paperMapper.queryByName(paperName);
             //存入缓存,时间3小时
            redisTemplate.opsForValue().set(key,p,3, TimeUnit.HOURS);
        }else{
            //缓存中取数据
            p = (Paper) obj;
        }

        ResultVo rv = new ResultVo();
        rv.setSuccess(true);
        rv.setData(p);
        return rv;
    }

    @Override
    public ResultVo countScore(Paper p) {
        ResultVo rv = new ResultVo();
        //定义key
        String key ="exam:"+p.getPaperName();
        //从缓存取出试卷信息
        Paper paper = (Paper)redisTemplate.opsForValue().get(key);
        //三种题型的正确答案集合
        List<PaperQuestion> signList = new LinkedList();
        List<PaperQuestion> moreList = new LinkedList();
        List<PaperQuestion> judgeList = new LinkedList();
        for(PaperQuestion pq:paper.getList()){
            if(pq.getQuType().equals("单选题")){
                signList.add(pq);
            } else if(pq.getQuType().equals("多选题")){
                moreList.add(pq);
            } else{
                judgeList.add(pq);
            }
        }
        //把考生答案转换成数组类型
        String[] signArr = p.getSa().split(",");
        String[] moreArr = p.getMa().split(",");
        String[] judgeArr = p.getJa().split(",");

        //计算总分
        int score=0;
        //评选单选题答案
        for(int i=0;i<signList.size();i++){
            if(signList.get(i).getQuAnswer().equals(signArr[i])){
                score+=signList.get(i).getQuScore();
            }
        }
        //评选多选题答案
        for(int i=0;i<moreList.size();i++){
            if(moreList.get(i).getQuAnswer().equals(moreArr[i])){
                score+=moreList.get(i).getQuScore();
            }
        }
        //评选判断题答案
        for(int i=0;i<judgeList.size();i++){
            if(judgeList.get(i).getQuAnswer().equals(judgeArr[i])){
                score+=judgeList.get(i).getQuScore();
            }
        }

        rv.setSuccess(true);
        rv.setData(score);
        return rv;
    }
}
